﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;
namespace QLKS
{
    public partial class FrmTimKiemThietBi : Form
    {
        private IList<THIETBI_DV> lstThietBi;

        public THIETBI_DV ThietBi;
        public FrmTimKiemThietBi()
        {
            InitializeComponent();
        }

        public FrmTimKiemThietBi(string TenThietBi)
        {
            InitializeComponent();
            InitThietBi(TenThietBi);
            txttimkiem.Text = TenThietBi;
        }

        private void InitThietBi(string TenThietBi)
        {
            THIETBI_DV[] outThietBi;
            var client = new ServiceQLKSClient();
            var error = client.doparserThietBi(out outThietBi, OPTCodeQLKS.OPTCode_THIETBI_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_THIETBI_GETALL_SUCCESS)
            {
                IList<THIETBI_DV> lsttmp;
                lstThietBi = outThietBi.ToList();
                lsttmp = lstThietBi.Where(x => x.TENTHIETBI.Contains(TenThietBi)).ToList();
                tHIETBIDVBindingSource.DataSource = lsttmp;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<THIETBI_DV> lsttmp;
            lsttmp = lstThietBi.Where(x => x.TENTHIETBI.Contains(txttimkiem.Text)).ToList();
            tHIETBIDVBindingSource.DataSource = lsttmp;
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            IList<THIETBI_DV> lsttmp;
            lsttmp = lstThietBi.Where(x => x.TENTHIETBI.Contains(txttimkiem.Text)).ToList();
            tHIETBIDVBindingSource.DataSource = lsttmp;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ThietBi = dataGridView1.CurrentRow.DataBoundItem as THIETBI_DV;
            this.Close();
        }


    }
}
